/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制
 */
package co.yixiang.controller.buyer.article;

import cn.hutool.core.util.ObjectUtil;
import co.yixiang.common.exception.BadRequestException;
import co.yixiang.common.utils.ResultUtil;
import co.yixiang.common.vo.ResultMessage;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.mp.entity.domain.YxArticle;
import co.yixiang.modules.mp.entity.domain.YxArticleCategory;
import co.yixiang.modules.mp.entity.domain.YxArticleVisitLog;
import co.yixiang.modules.mp.entity.dto.*;
import co.yixiang.modules.mp.entity.vo.YxArticleCategoryVoF;
import co.yixiang.modules.mp.entity.vo.YxArticleVo;
import co.yixiang.modules.mp.service.YxArticleCategoryService;
import co.yixiang.modules.mp.service.YxArticleService;
import co.yixiang.modules.mp.service.YxArticleVisitLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * @author hupeng
 * @date 2019-10-07
 */
@Api(tags = "商城:图文管理")
@RestController
@RequestMapping("/article")
public class ArticleBuyerController {

    @Autowired
    private YxArticleService yxArticleService;
    @Autowired
    private YxArticleCategoryService articleCategoryService;
    @Autowired
    private YxArticleVisitLogService articleVisitLogService;

    @ApiOperation("获取文章分类")
    @GetMapping(value = "/category")
    public ResultMessage<List<YxArticleCategoryVoF>> articleCategorys(YxArticleCategoryQueryCriteria criteria) {
        final List<YxArticleCategoryVoF> yxArticleCategories = articleCategoryService.queryAllByVo(criteria);
        return ResultUtil.data(yxArticleCategories);
    }

    @ApiOperation(value = "分页查询文章数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cid", value = "分类ID", paramType = "query"),
    })
    @GetMapping
    public ResultMessage<Map<String, Object>> getYxArticles(YxArticleQueryCriteria criteria, Pageable pageable) {
        final Map<String, Object> stringObjectMap = yxArticleService.queryAllToVo(criteria, pageable);
        return ResultUtil.data(stringObjectMap);
    }

    @ApiOperation(value = "查询可首页展示的新闻")
    @GetMapping(value = "/home")
    public ResultMessage<List<YxArticleVo>> getHome(YxArticleQueryCriteria criteria, Pageable pageable) {
        final List<YxArticleVo> yxArticles = yxArticleService.queryAllToHome(criteria);
        return ResultUtil.data(yxArticles);
    }

    @ApiOperation(value = "阅读文章")
    @GetMapping(value = "/read/{articleId}")
    public ResultMessage<YxArticleVo> readYxArticles(@PathVariable Integer articleId) {
        if(ObjectUtil.isEmpty(articleId)) {
            throw new BadRequestException("查询参数错误:文章ID不能为空");
        }
        YxArticleVo articleVo = yxArticleService.readArticleVo(articleId);
        return ResultUtil.data(articleVo);
    }

    @ApiOperation(value = "修改文章浏览记录",notes = "浏览时长、次数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "记录ID",required = true, paramType = "query"),
            @ApiImplicitParam(name = "maxTime", value = "浏览时长", paramType = "query")
    })
    @PutMapping(value = "/visitLog")
    public ResultMessage<String> update(@Validated @RequestBody YxArticleVisitLog resources) {
        YxArticleVisitLog byId = articleVisitLogService.getById(resources.getId());
        byId.setMaxTime(resources.getMaxTime()> byId.getMaxTime() ? resources.getMaxTime() : byId.getMaxTime());
        byId.setTotalTime(byId.getTotalTime() + resources.getMaxTime());
        byId.setVisit(byId.getVisit() + 1);
        articleVisitLogService.updateById(byId);
        return ResultUtil.success();
    }


    @ApiOperation(value = "修改文章浏览记录",notes = "点赞状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "记录ID",required = true, paramType = "query"),
            @ApiImplicitParam(name = "likeStatus", value = "点赞状态", paramType = "query")
    })
    @PutMapping(value = "/likeStatus")
    public ResultMessage<String> likeStatus(@Validated @RequestBody YxArticleVisitLog resources) {
        YxArticleVisitLog byId = articleVisitLogService.getById(resources.getId());
        byId.setLikeStatus(resources.getLikeStatus());
        articleVisitLogService.updateById(byId);
        return ResultUtil.success();
    }

}
